﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi.Client;
using System.Timers;
using System.Windows.Threading;
using System.Threading;
namespace Yugi.Client.Control
{
    public partial class YPhase : UserControl, IInvoke
    {
        public TurnModel model;
        Image[] k = new Image[6];
        int curent = -1;
        int select = -1;
        int goal = -1;
        public YPhase()
        {
            this.InitializeComponent();
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(YPhase_DataContextChanged);
            k[0] = dp;
            k[1] = sp;
            k[2] = mp;
            k[3] = bp;
            k[4] = mp2;
            k[5] = ep;
            SetP("p0");
        }
        void YPhase_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            model = this.DataContext as TurnModel;
            if (model == null) return;
            model.Presenter = this;
        }
        #region IInvoke Members

        public void Do(ClientMSG msg, params object[] obj)
        {
            curent = getindex(obj[0].ToString());
            if (model.YSupervisorModel.AllowSkip)
            {
                SetP("p0");
                TurnOn("p0", curent);
                //chay chu o day( curent)
                if (curent < goal)
                {
                    ((Yugi_Gui.Game1Layout)(model.YSupervisorModel.win)).Skip_Button_Click(null, null);
                }
            }
            else
            {
                SetP("p1");
                TurnOn("p1", curent);
                goal = -1;
            }
        }

        private int getindex(string p)
        {
            switch (p)
            {
                case "DrawPhase": return 0;
                case "StandbyPhase": return 1;
                case "MainPhase1": return 2;
                case "BattlePhase": return 3;
                case "MainPhase2": return 4;
                case "EndTurn": return 5;
                case "dp": return 0;
                case "sp": return 1;
                case "mp": return 2;
                case "bp": return 3;
                case "mp2": return 4;
                case "ep": return 5;
            }
            return 0;
        }
        public void Refresh()
        {

        }
        #endregion
        public void SetP(string p)
        {
            for (int i = 0; i < 6; i++)
            {
                if ((i != select) || p == "p1")
                {
                    string s = "/Yugi_Gui;component/Image/Particles/Phase/" + k[i].Name + "_" + p + ".png";
                    k[i].Source = new BitmapImage(new Uri(s, UriKind.Relative));
                }
            }
        }
        public void TurnOn(string p, int index)
        {
            string s = "/Yugi_Gui;component/Image/Particles/Phase/on_" + k[index].Name + "_" + p + ".png";
            k[index].Source = new BitmapImage(new Uri(s, UriKind.Relative));
        }
        public void TurnOff(string p, int index)
        {
            string s = "/Yugi_Gui;component/Image/Particles/Phase/" + k[index].Name + "_" + p + ".png";
            k[index].Source = new BitmapImage(new Uri(s, UriKind.Relative));
        }

        private void ep_MouseEnter(object sender, MouseEventArgs e)
        {
            if (model != null)
                if (allowclick(sender))//luot cua p0
                {
                    select = getindex(((Image)sender).Name);
                    string s = "/Yugi_Gui;component/Image/Particles/Phase/on_" + ((Image)sender).Name + "_p0.png";
                    ((Image)sender).Source = new BitmapImage(new Uri(s, UriKind.Relative));
                }
        }
        public bool allowclick(object sender)//chua clock cac truong hop khac
        {
            if (model.YSupervisorModel.AllowSkip && getindex(((Image)sender).Name) > curent)
                return true;
            return false;
        }
        private void ep_MouseLeave(object sender, MouseEventArgs e)
        {
            select = -1;
            if (model != null)
                if (allowclick(sender))
                {
                    string s = "/Yugi_Gui;component/Image/Particles/Phase/" + ((Image)sender).Name + "_p0.png";
                    ((Image)sender).Source = new BitmapImage(new Uri(s, UriKind.Relative));
                }
        }

        private void ep_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)//cach chuyen tu cac image thanh message
        {
            if (model != null)
            {
                if (allowclick(sender))
                {
                    ((Yugi_Gui.Game1Layout)(model.YSupervisorModel.win)).Skip_Button_Click(null, null);
                    goal = getindex(((Image)sender).Name);
                }
            }
        }


    }
}